package com.waitingresult;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTPayload;
import cn.hutool.jwt.JWTUtil;
import lombok.Data;

import java.util.Map;

/**
 * @Author: WangShuai
 * @CreateTime: 2022-10-26  14:35
 * @Description: TODO
 * @Version: 1.0
 */
public class JWTDemo {
    @Data
    static class User{
        private Long id;
        private String name;
    }
    // 1xn
    public static void main(String[] args) {
        User user = new User();
        user.setId(1L);
        user.setName("陆远");
        String UserStr = JSONUtil.toJsonStr(user);
        Map map1 = JSONUtil.toBean(UserStr, Map.class);
        String token = JWTUtil.createToken(map1, "test".getBytes());
        System.err.println(token);
        // eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoi6ZmG6L-cIiwiaWQiOjF9.ykFfm5EisKqt3Xt7JHulahuEh88FQOc7_FxKbyEhLeU
        JWT jwt = JWTUtil.parseToken("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoi6ZmG6L-cIiwiaWQiOjF9.ykFfm5EisKqt3Xt7JHulahuEh88FQOc7_FxKbyEhLeU");
        JWTPayload payload = jwt.getPayload();
        JSONObject claimsJson = payload.getClaimsJson();
        User user1 = JSONUtil.toBean(claimsJson, User.class);


        System.err.println(user1);
    }
}
